package com.demo.service.impl;

import com.demo.mapper.RepairMapper;
import com.demo.mapper.ZcMapper;
import com.demo.pojo.Repair;
import com.demo.pojo.Zc;
import com.demo.service.RepairService;
import com.demo.utils.DateUtils;
import com.demo.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author： Administrator
 * @date: 21/05/14 09:43
 * 描述：
 */
@Service
public class RepairServiceImpl implements RepairService {
    @Autowired
    private RepairMapper repairMapper;
    @Autowired
    private ZcMapper zcMapper;

    @Override
    public int add(Repair repair) {
        return 0;
    }

    @Override
    public int del(String no) {
        return 0;
    }

    @Override
    public int update(Repair repair) {
        return 0;
    }

    @Override
    public Page<Repair> page(Repair repair, int pageNo, int pageSize) {
        return null;
    }

    @Override
    public int del(Integer id) {
        return 0;
    }

    @Override
    public int send(Repair repair, String username) {
        Integer zcId = repair.getZcId();
        Zc zc = zcMapper.selectByPrimaryKey(zcId);
        if (zc == null) {
            return -1;
        }
        if (!"正常".equals(zc.getZcState())) {
            return -2;
        }
        zc.setZcState("维修");
        if (zcMapper.updateByPrimaryKeySelective(zc) != 1) {
            return -3;
        }
        repair.setSendTime(DateUtils.format(new Date()));
        repair.setLoginUser(username);
        return repairMapper.insertSelective(repair);
    }

    @Override
    public int xw(Repair repair) {
        Repair select = repairMapper.selectByPrimaryKey(repair.getRepairNo());
        if (select == null) {
            return -1;
        }
        if (select.getWxTime() != null) {
            return -2;
        }
        Integer zcId = select.getZcId();
        Zc zc = zcMapper.selectByPrimaryKey(zcId);
        if (!"维修".equals(zc.getZcState())) {
            return -3;
        }
        zc.setZcState("正常");
        if (zcMapper.updateByPrimaryKeySelective(zc) != 1) {
            return -4;
        }
        return repairMapper.updateByPrimaryKeySelective(repair);
    }

    @Override
    public Page<Repair> page(String wx, int pageNo, int pageSize) {
        Page<Repair> page = new Page<>();
        page.setPageSize(pageSize);
        int count = repairMapper.pageCount(wx);
        page.setPageTotalCount(count);
        int total = count / pageSize;
        if (count % pageSize > 0) {
            total++;
        }
        page.setPageTotal(total);
        page.setPageNo(pageNo);
        List<Repair> repairs = repairMapper.pageByCond(wx, (pageNo - 1) * pageSize, pageSize);
        page.setItems(repairs);
        return page;
    }
}
